package com.itheima.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.itheima.bean.Student;
import com.itheima.service.StudentService;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

/*@Controller
@ResponseBody *///表示该类的所有方法都不进行页面跳转，通过响应体响应数据
@RestController  //等价于 @Controller+@ResponseBody
@RequestMapping("/student")
public class StudentController{

    @Autowired  //告诉spring对象在创建bean对象时给属性赋值
    private StudentService studentService;

    //注释掉之后，那么就需要考虑在哪里加载applicationContext.xml配置文件?
    /*@Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
        studentService = ac.getBean("studentService", StudentService.class);
    }*/
    /**
     * 处理分页查询的请求
     */
    //@RequestMapping("/selectByPage")
    @GetMapping("/{currentPage}/{pageSize}")
    protected PageInfo<Student> selectByPage(@PathVariable Integer currentPage, @PathVariable Integer pageSize){
        //2 调用service层方法获取结果PageInfo对象
        return studentService.selectByPage(currentPage, pageSize);
    }
    /**
      * 处理添加学生的请求
     */
    //@RequestMapping("/addStudent")
    @PostMapping
    protected Integer addStudent(@RequestBody Student student) {
        //2 调用service层方法，添加学生信息
        return studentService.addStudent(student);
    }
    /**
     * 处理修改学生的请求
     */
    //@RequestMapping("/updateStudent")
    @PutMapping
    protected Integer updateStudent(@RequestBody Student student) {
        //2 调用service层方法，修改学生信息
        return studentService.updateStudent(student);
    }
    /**
     * 处理删除学生的请求
     */
    //@RequestMapping("/deleteStudent")
    @DeleteMapping("/{number}")
    protected Integer deleteStudent(@PathVariable String number) {
        //2 调用service层方法，删除学生信息
        return studentService.deleteStudent(number);
    }
}
